package com.itheima.algorithm.redblacktree;

public class Node{

    public int key;
    public Object value;
    public Node left;
    public Node  right; // 父节点
    public Node parent; // 颜色
    public Color color = Color.RED;

    public Node(int key, Object value) {
        this.key = key;
        this.value = value;
    }

    public Node(int key) {
        this.key = key;
    }

    public Node(int key, Color color) {
        this.key = key;
        this.color = color;
    }

    public Node(int key, Color color, Node left, Node right) {
        this.key = key;
        this.color = color;
        this.left = left;
        this.right = right;
        if (left != null) {
            left.parent = this;
        }
        if (right != null) {
            right.parent = this;
        }
    }

    // 是否是左孩子
    boolean isLeftChild() {
        return parent != null && parent.left == this;
    }

    // 叔叔
    Node uncle() {
        if (parent == null || parent.parent == null) {
            return null;
        }
        if (parent.isLeftChild()) {
            return parent.parent.right;
        } else {
            return parent.parent.left;
        }
    }

    // 兄弟
    Node sibling() {
        if (parent == null) {
            return null;
        }
        if (this.isLeftChild()) {
            return parent.right;
        } else {
            return parent.left;
        }
    }

}